package com.ruoyi.web.controller.api;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.UtilTool;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author jiebin
 * @date 2021/7/15 16:34
 */
@Slf4j
@RestController
@RequestMapping("/api/web")
public class WebSiteApiController extends BaseController {

    @Autowired
    private IWebCateService webCateService;
    @Autowired
    private IWebSiteService webSiteService;
    @Autowired
    private CommoneService commoneService;
    @Autowired
    private IAppAboutService appAboutService;
    @Autowired
    private IAppPushService appPushService;
    @Autowired
    private IAppUserPushService appUserPushService;
    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private IAppUserAboutService appUserAboutService;

    /**
     * 查询默认网址列表
     */
    @PostMapping("/getSiteList")
    @ResponseBody
    public TableDataInfo getSiteList(WebSite webSite) {
        startPage();
        List<WebSite> list = webSiteService.getSubscribeList(webSite);
        return getDataTable(list);
    }

    /**
     * 添加网址
     * @param webSite
     * @return
     */
    @PostMapping("/addSite")
    @ResponseBody
    public AjaxResult addSite(WebSite webSite) {
        String url = webSite.getWebUrl();
        String logoUrl = UtilTool.checkUrl(url);
        WebSite site = webSiteService.getOneWebSite(webSite);
        if (!UtilTool.isNull(site)){
            return AjaxResult.error("网址已经存在",site);
        }
        if (!UtilTool.isNull(logoUrl)){
            webSite.setLogoUrl(logoUrl);
        }
        webSite.setWebUrl(url);
        webSite.setId(commoneService.getWebId());
        webSite.setSerialNum(UtilTool.getWebSerialNum());
        webSite.setSource(1);
        webSite.setCreateTime(new Date(System.currentTimeMillis()));
        webSite.setUpdateTime(new Date(System.currentTimeMillis()));
        //return toAjax(webSiteService.insertWebSite(webSite));
        webSiteService.insertWebSite(webSite);
        return AjaxResult.success(webSite);
    }

    /**
     * 检验网址是否存在
     * @param webSite
     * @return
     */
    @PostMapping("/checkSite")
    @ResponseBody
    public AjaxResult checkSite(WebSite webSite) {
        WebSite site = webSiteService.getOneWebSite(webSite);
        if (!UtilTool.isNull(site)){
            return AjaxResult.error("网址已经存在",site);
        }
        return AjaxResult.success("网址不存在");
    }

    /**
     * 更新网址
     * @param webSite
     * @return
     */
    @PostMapping("/updateSite")
    @ResponseBody
    public AjaxResult updateSite(WebSite webSite) {
        WebSite site = webSiteService.selectWebSiteById(webSite.getId());
        if (UtilTool.isNull(site)){
            return AjaxResult.success("该链接为空");
        }
        webSite.setUpdateTime(new Date(System.currentTimeMillis()));
        webSiteService.updateWebSite(webSite);
        return AjaxResult.success(webSite);
    }

    /**
     * 统计网址浏览量
     * @param webSite
     * @return
     */
    @PostMapping("/statisticalWebUrl")
    @ResponseBody
    public AjaxResult statisticalWebUrl(WebSite webSite){
        webSite = webSiteService.selectWebSiteById(webSite.getId());
        if (!UtilTool.isNull(webSite)){
            webSite.setClickNum(webSite.getClickNum()+1);
            webSiteService.updateWebSite(webSite);
            return AjaxResult.success(webSite.getClickNum());
        }else {
            return AjaxResult.error("该网址为空");
        }
    }

    /**
     * 获取后台自定义的分类
     * @param cate
     * @return
     */
    @PostMapping("/getCateList")
    @ResponseBody
    public AjaxResult getCateList(WebCate cate) {
        cate.setSource(0);
        List<WebCate> list = webCateService.selectWebCateList(cate);
        return AjaxResult.success(list);
    }

    /**
     * 获取后台固定推送
     * @return
     */
    @PostMapping("/getFixedPush")
    @ResponseBody
    public AjaxResult getFixedPush(AppUserPush appUserPush) {
        checkPush(new AppPush());
        AppUserAbout appUserAbout = new AppUserAbout();
        if (!UtilTool.isNull(appUserPush.getUserName())){
            appUserAbout.setUserName(appUserPush.getUserName());
        }else {
            appUserAbout.setUuid(appUserPush.getUuid());
        }
        List<AppUserAbout> appUserAbouts = appUserAboutService.selectAppUserAboutList(appUserAbout);
        if (appUserAbouts.size()>0){
            return AjaxResult.error();
        }else {
            int number = getNumber(appUserPush);
            AppAbout about = appAboutService.selectAppAboutById(1l);
            if (UtilTool.isNull(about)){
                about = new AppAbout();
                about.setRemark("0");
            }else {
                about.setRemark(""+number);
            }
            return AjaxResult.success(about);
        }
    }

    /**
     * app删除固定推送
     * @return
     */
    @PostMapping("/delPush")
    @ResponseBody
    public AjaxResult delPush(AppUserAbout appUserAbout) {
        AppUserPush appUserPush = new AppUserPush();
        if (!UtilTool.isNull(appUserAbout.getUserName())){
            appUserPush.setUserName(appUserAbout.getUserName());
        }else {
            appUserPush.setUuid(appUserAbout.getUuid());
        }
        appUserPushService.del(appUserPush);
        List<AppUserAbout> appUserAbouts = appUserAboutService.selectAppUserAboutList(appUserAbout);
        if (appUserAbouts.size()==0){
            appUserAbout.setAboutId(1L);
            appUserAboutService.insertAppUserAbout(appUserAbout);
        }
        return AjaxResult.success();
    }

    /**
     * 获取后台固定推送列表
     * @return
     */
    @PostMapping("/getPushList")
    @ResponseBody
    public TableDataInfo getPushList(AppPush appPush, HttpServletRequest request) {
        String uuid = request.getHeader("uuid");
        String userName = request.getHeader("userName");
        appPush.setState(1);
        startPage();
        List<AppPush> list = appPushService.selectAppPushList(appPush);
        AppUserPush appUserPush = new AppUserPush();
        if (UtilTool.isNull(uuid)){
            appUserPush.setUserName(userName);
        }else {
            appUserPush.setUuid(uuid);
        }
        for (AppPush push:list){
            appUserPush.setPushId(push.getId());
            List<AppUserPush> appUserPushs = appUserPushService.selectAppUserPushList(appUserPush);
            push.setRemark(appUserPushs.size()+"");
        }
        return getDataTable(list);
    }

    /**
     * 通过id获取推送详情
     * @return
     */
    @PostMapping("/getPushDetailById")
    @ResponseBody
    public AjaxResult getPushDetailById(AppPush appPush) {
        appPush = appPushService.selectAppPushById(appPush.getId());
        if (UtilTool.isNull(appPush)){
            return AjaxResult.error("该数据为空");
        }else {
            List<WebSite> list = webSiteService.getWebSiteByIds(appPush.getSiteIds());
            return AjaxResult.success(list);
        }
    }

    /**
     * 阅读回调删除推送记录
     * @param appUserPush
     * @return
     */
    @PostMapping("/readPush")
    @ResponseBody
    public AjaxResult readPush(AppUserPush appUserPush){
        if (UtilTool.isNull(appUserPush.getUserName()) && UtilTool.isNull(appUserPush.getUuid())){
            return AjaxResult.error("用户参数不能为空");
        }
        if (UtilTool.isNull(appUserPush.getPushId())){
            return AjaxResult.error("推送id不能为空");
        }
        return toAjax(appUserPushService.del(appUserPush));
    }

    //添加AppUserPush记录
    private void addAppUserPush(long pushId){
        List<AppUser> appUsers = appUserService.selectAppUserList(new AppUser());
        for (AppUser user:appUsers){
            AppUserAbout appUserAbout = new AppUserAbout();
            AppUserPush appUserPush = new AppUserPush();
            // type 0默认注册用户 1手机唯一码用户
            if (user.getType()==0){
                appUserPush.setUserName(user.getUserName());
                appUserAbout.setUserName(user.getUserName());
            }else {
                appUserPush.setUuid(user.getUuid());
                appUserPush.setUuid(user.getUuid());
            }
            appUserPush.setPushId(pushId);
            appUserPushService.insertAppUserPush(appUserPush);
            List<AppUserAbout> appUserAbouts = appUserAboutService.selectAppUserAboutList(appUserAbout);
            for (AppUserAbout about:appUserAbouts){
                appUserAboutService.deleteAppUserAboutById(about.getId());
            }
        }
    }

    /**
     * 查询是否有没有更新的通知
     * @param appPush
     */
    private void checkPush(AppPush appPush){
        appPush.setState(0);
        List<AppPush> list = appPushService.selectAppPushList(appPush);
        for (AppPush push:list){
            if (!UtilTool.isNull(push.getEffectTime())){
                if (push.getEffectTime().getTime()<System.currentTimeMillis()){
                    push.setState(1);
                    push.setUpdateTime(new Date(System.currentTimeMillis()));
                    appPushService.updateAppPush(push);
                    addAppUserPush(push.getId());
                }
            }
        }
    }

    private int getNumber(AppUserPush appUserPush){
        List<AppUserPush> list = appUserPushService.selectAppUserPushList(appUserPush);
        return list.size();
    }

    /**
     * 通过ids查询网址列表
     */
    @PostMapping( "/getWebSiteByIds")
    @ResponseBody
    public AjaxResult getWebSiteByIds(String ids) {
        List<WebSite> list = webSiteService.getWebSiteByIds(ids);
        return AjaxResult.success(list);
    }

    /**
     * app百宝箱搜索
     */
    @PostMapping( "/getSearchList")
    @ResponseBody
    public AjaxResult getSearchList(WebSite webSite) {
        List<WebSite> list = webSiteService.getSearchList(webSite);
        return AjaxResult.success(list);
    }

}
